package com.iot.dao.impl;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iot.base.Basic;
import com.iot.dao.RedisDao;
import com.iot.domain.Album;
import com.iot.domain.AlbumVo;
import com.iot.domain.MvFile;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;


@Component
public class RedisDaoImpl implements RedisDao {

    @Autowired
    private static final ObjectMapper MAPPER = new ObjectMapper();



    //redis 单例
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /*将文件名保存到redis 中*/
    @Override
    public void saveFile(Album album) throws JsonProcessingException {

        AlbumVo albumVo=new AlbumVo();
        //做一个 对象的拷贝
        BeanUtils.copyProperties(album,albumVo);

        System.out.println(albumVo);
        //获取redis 对象
        //将对象进行转化为json字符串  直接传到缓存中 分开存储   一个表存照片的信息  一个表存相册的其他的信息

        for (MvFile file:
           album.getPictures() ) {
            //将文件一个一个的序列化到redis中
            stringRedisTemplate.opsForSet().add(album.getName()+ Basic.PICTURES,MAPPER.writeValueAsString(file));
        }
            stringRedisTemplate.opsForValue().set(album.getName()+Basic.INFOS,MAPPER.writeValueAsString(albumVo));
    }


    @Override
    public Set<String> findAllPicture() {
        Set<String> keys = stringRedisTemplate.keys("*");

        if (keys != null) {
            return keys;
        }

        return null;
    }


    /*判断是否存在相册*/
    @Override
    public Boolean existAlbum(String albumName) {
        //判断是否存在该相册
        Boolean aBoolean = stringRedisTemplate.hasKey(albumName+Basic.INFOS);
        //如果存在就返回相册存在

        return  aBoolean;
    }

    /*通过相册名获取相册*/
    @Override
    public Album findPictureByAlbumName(String albumName) throws IOException {


        Album album = new Album();

        List<MvFile> fileList = new ArrayList<>();

        /*相册名为Honer  为荣誉墙的相策 时*/
        if (albumName.equals(Basic.HONERS)) {

            Set<String> members = stringRedisTemplate.opsForSet().members(albumName);


            //反序列化照片   封装成对象
            for (String file :
                    members) {
                System.out.println(file);
                MvFile mvfile = MAPPER.readValue(file, MvFile.class);
                System.out.println(mvfile);
                fileList.add(mvfile);
            }

         //将返回的结果设置到对象中
            album.setPictures(fileList);
            //对象拷贝
            return album;


        } else {

            //获取存储的照片
            Set<String> members = stringRedisTemplate.opsForSet().members(albumName + Basic.PICTURES);


            //反序列化照片   封装成对象
            for (String file :
                    members) {
                System.out.println(file);
                MvFile mvfile = MAPPER.readValue(file, MvFile.class);
                System.out.println(mvfile);
                fileList.add(mvfile);
            }


            System.out.println("xiangce:" + albumName + Basic.INFOS);
            //反序列化相册的信息
            String s = stringRedisTemplate.opsForValue().get(albumName + Basic.INFOS);

            System.out.println(s);
            AlbumVo albumVo = MAPPER.readValue(s, AlbumVo.class);

            album.setPictures(fileList);
            //对象拷贝
            BeanUtils.copyProperties(albumVo, album);
            return album;
        }
    }

    /*获取所有的相册*/
    @Override
    public List<AlbumVo> findAllAlbum()  {


        List<AlbumVo> list=new ArrayList<>();

        Set<String> keys = null;

        /*异常处理*/
        try {
            keys = stringRedisTemplate.keys("*INFOS");
            if (keys!=null){
                for (String key:
                        keys) {
                    String s = stringRedisTemplate.opsForValue().get(key);
                    //反序列化
                    AlbumVo albumVo = MAPPER.readValue(s, AlbumVo.class);

                    list.add(albumVo);
                }
            }
        } catch (Exception e) {
            return list;
        }

     return list;

    }


    /*给指定的相册上传照片*/
    @Override
    public void uploadPictureByName(String albumName,List<MvFile> files) throws JsonProcessingException {


        /*如果相册的名称为荣誉墙时   对相册的名字做出单独的处理*/
        if (albumName.equals(Basic.HONERS)){

            for (MvFile file:
                    files ) {
                //将文件一个一个的序列化到redis中
                stringRedisTemplate.opsForSet().add(albumName,MAPPER.writeValueAsString(file));
              }

        }else {

        for (MvFile file:
                files ) {
            //将文件一个一个的序列化到redis中
            stringRedisTemplate.opsForSet().add(albumName+Basic.PICTURES,MAPPER.writeValueAsString(file));
          }
        }
    }

}
